<?php


require_once(dirname(__FILE__).'/../../common/log.php');
require_once(dirname(__FILE__).'/../../common/defines.php');
require_once(dirname(__FILE__).'/../../common/return.php');
require_once(dirname(__FILE__).'/../../database/mysql.class.php');
require_once(dirname(__FILE__).'/../../database/redis.class.php');

require_once(dirname(__FILE__).'/push_message.php');


$body  = file_get_contents('php://input');
logd("recv: ".$body);
$data  = json_decode($body, true);

if ((!isset($data["userId"])) || (!isset($data['title'])) || (!isset($data['content'])))
{
    failure(ERR_PARAM);
}

$mysql  = new mysqldb();
$ret    = $mysql->open_db();
if ($ret === FAILURE)
{
    failure(ERR_SYSTEM);
}

$ret  = $mysql->read('SELECT onlineStatus,udid FROM user_login_info uli INNER JOIN user_base_info ubi ON uli.userId=ubi.id AND uli.userId='.$data['userId']);
if ($ret === FAILURE)
{
    failure(ERR_SYSTEM);
}

if ((!is_array($ret)) || empty($ret))
{
    logw(['用户信息错误', $ret]);

    failure(-10, '用户信息错误');
}

if (empty($ret[0]['udid']))
{
    failure(-10, 'udid 无效');
}

$ret  = jpush($ret[0]["udid"], $data['title'], $data['content']);
if ($ret == 0)
{
    success();
}
else
{
    failure(-10, '发送失败');
}

